//<reference path="./stumsUtil.js" />
//通用翻页
$(document).ready(function () {  	
	//第一页
	$("#_cfirstPage").click(function(){
		$("#_cpage").attr("value",1);
		var params  =  pageParam();
		if(params == null || params == ""){
			
		}
		queryAjax(params,"firstPage");
    });
	
	 //最后一页
	$("#_clastPage").click(function(){
		var lastNumber = $("#_cpageCount").val();
		$("#_cpage").attr("value",lastNumber);
		var params  =  pageParam();
		queryAjax(params,"lastPage");
    });
	
	//上一页
	$("#_cprecPage").click(function(){
		if(parseInt($("#_cpage").val()) > 1){
		  $("#_cpage").attr("value",parseInt($("#_cpage").val()) - 1);
		  var params  =  pageParam();
		  queryAjax(params,"precPage");
		}else{
		  alert("第一页！");
		}
    });
	
	//下一页
    $("#_cnextPage").click(function(){
    	$("#_cpage").attr("value",parseInt($("#_cpage").val()) + 1);
    	var params  =  pageParam();
		queryAjax(params,"nextPage");
    });
    
    //获取页面查询字段参数
    var pageParam = function(){
    	var param = "";
    	$("input[type='text'][id^='_cparam']").map(function() {
		   	  if(this.value != null && this.value != ""){
  				  param = param + this.name + "=" + this.value + "&";
  			  }else{
  				  //param = param + this.name+ "=" +null + "&";
  			  }		  
    	});
    	var temp = titleParams();
    	
    	param = param + "array=" + temp + "&pageNumber=" + $("#_cpageNumber").val() + "&page=" + $("#_cpage").val();
    	return param;
    }
    
    //获取页面显示字段参数
    var titleParams = function(){
    	//th中的id值
    	var titleParams = "";
    	$("._cparamTitle").map(function() {
    		   if(this.id != null && this.id != ""){
  				   titleParams = titleParams + this.id+",";
  			   }	  
    	 });
    	titleParams = titleParams.substring(0,titleParams.length - 1);
    	return titleParams;
    }
	
    //页面查询功能
    var queryAjax = function(params,action){
    	    var url = $("#_cpageUrl").val();
    	    var page="";
    	    var pageNumber = "";
    	    var rebol = "false";
    	    var chosPart = $("#_cchosPart").val();
			$.ajax({  
	            url:url,  
	            type:'post',  
	            data:params,  
	            dataType:'json',  
	            success:function (data) { 
						if(data != null && data !=""){
							        $.each(data,function(key,list) {
							        	if(key == "result" && list !=null && list != "" ){
						        			$(".dataArea").remove();
						        			rebol = "true";
						        			var array = titleParams().split(",");
											$.each(list,function(m,obj) { 
												var row = "<tr class=dataArea><td>" + (m+1)+"</td>" ;
												$.each(obj,function(i,ob){
													 if( i == 0){
														row  += "<td><input type=checkbox  name=_ckId id=_ckId  value=" +ob +" /> </td>";
													 }else{
														 row += "<td>" +ob+ "</td>";
													 }
												});
											    row += "</tr>";
											    $("#dataTable").append(row);
											});
							        	}
							        	if(key == "page"){
							        		page = list;
							        	}
							        	if(key == "pageCount"){
							        		pageCount = list;
							        	}
							        	if(key == "pageNumber"){
							        		pageNumber = list;
							        	}
							});
							
			 			}
						//判断页数
						if(rebol  == "true" ){
							$("#_cpageCount").attr("value",pageCount);
							$("#_cpage").attr("value",page);
							$("#_cpageNumber").attr("value",pageNumber);
						 }else{  
			 				$("#_cpageCount").attr("value",pageCount);
			 				$("#_cpageNumber").attr("value",pageNumber);
				 			if( action == "firstPage" ){
				 				alert("查询结果为空");
							}else if(action == "lastPage"){
								$("#_cpage").attr("value",parseInt($("#_cpage").val()));
							}else{
								$("#_cpage").attr("value",parseInt($("#_cpage").val()) - 1);
							}
				 			
			 			}
	            },
	            error:function(xhr){alert(xhr.responseText);}  
	        });  
    }
    
    //全选
    $("#_checkall").click(function(){ 
		if(this.checked){ 
			$("input[type='checkbox'][ id='_ckId']").each(function(){
				this.checked=true;
			}); 
		}else{ 
			$("input[type='checkbox'][ id='_ckId']").each(function(){
					this.checked=false;
			}); 
		}
	});
    
    //选择
    var checkParam = function(){
    	 var param = "";
    	 $("_ckId").map(function() {
    		if( this.checked == true){
  			 	param += this.value + ",";
  			}
    	 });
    	 param = param.substring(0,param.length - 1);
    	 return param;
    }
    
    //搜索功能
    $("#_ctableSearch").click(function(){
		$("#_cpage").attr("value",1);
		var params  =  pageParam();
		queryAjax(params,"firstPage");
    });
    
     $("#_cpageNumber").change(function(){
  		$("#_cpage").attr("value",1);
		var params  =  pageParam();
		queryAjax(params,"firstPage");
	});

}); 


 
